// server/server-function-hub.js
/**
 * Promise-Aware Server Function Hub
 * Enhanced to properly handle async functions and Promises
 */
const WebSocket = require('ws');
const https = require('https');
const fs = require('fs');
const path = require('path');
require('dotenv').config();

// Configuration
const WS_PORT = process.env.WS_PORT || 8080;
const SSL_PREFIX = process.env.SSL_PREFIX || '/etc/letsencrypt/live/';
const DOMAIN = process.env.DOMAIN || 'tuvme.xyz';

// SSL Certificate paths
const SSL_KEY_PATH = process.env.SSL_KEY_PATH || path.join(SSL_PREFIX, DOMAIN, 'privkey.pem');
const SSL_CERT_PATH = process.env.SSL_CERT_PATH || path.join(SSL_PREFIX, DOMAIN, 'fullchain.pem');

/**
 * Enhanced logging function
 */
function log(label, data = null) {
  const timestamp = new Date().toISOString();
  console.log(`[${timestamp}] [SERVER-FUNCTION-HUB] ${label}`);
  if (data) {
    console.log(JSON.stringify(data, null, 2));
  }
}

/**
 * In-memory store for functions
 */
const functionStore = new Map();

/**
 * Stats
 */
const stats = {
  totalConnections: 0,
  activeConnections: 0,
  registrations: 0,
  executions: 0,
  errors: 0
};

/**
 * Register a function or code snippet
 */
function registerFunction(functionId, functionBody, paramNames = []) {
  stats.registrations++;
  
  // Log full function body without any truncation
  log(`======= REGISTERING FUNCTION: ${functionId} =======`);
  console.log("FUNCTION BODY BEGIN:");
  console.log(functionBody);
  console.log("FUNCTION BODY END");
  log(`========================================`);

  // Store the code info without any validation
  functionStore.set(functionId, {
    body: functionBody,
    params: paramNames
  });

  log(`Code successfully registered: ${functionId}`);
  return true;
}

/**
 * Helper function to check if a value is a Promise or Promise-like
 */
function isPromiseLike(value) {
  return value && typeof value.then === 'function';
}

/**
 * Execute registered function
 */
async function executeFunction(functionId, args = []) {
  stats.executions++;
  
  log(`======= EXECUTING FUNCTION: ${functionId} =======`);
  log(`Arguments:`, args);

  if (!functionStore.has(functionId)) {
    log(`Code not found: ${functionId}`);
    throw new Error(`Code not found: ${functionId}`);
  }

  try {
    const funcInfo = functionStore.get(functionId);

    // Log full stored function body
    log(`EXECUTING THIS EXACT CODE:`);
    console.log(funcInfo.body);
    log(`========================================`);

    // Create execution context
    const executeCode = (code, args) => {
      // Create a safe execution context with allowed Node.js modules
      const vm = require('vm');
      
      const sandbox = {
        require,
        process,
        console,
        Buffer,
        setTimeout,
        clearTimeout,
        setInterval,
        clearInterval,
        setImmediate,
        clearImmediate,
        args,
        Promise // Explicit Promise reference
      };
      
      // Create context and run code
      const context = vm.createContext(sandbox);
      
      // FIXED: Instead of double-wrapping in IIFEs, check if the code is already
      // returning a Promise or has an async function signature.
      // If the code appears to be async, we'll just run it directly.
      let codeToRun = code;
      
      // Check if code appears to be an async IIFE already
      const isAsyncIIFE = /^\s*\(?\s*async\s+function/.test(code) || 
                           code.includes('new Promise');
      
      // If not an async IIFE already, but is a plain function or statement, wrap it
      if (!isAsyncIIFE) {
        log(`Code is not an async IIFE, wrapping in function...`);
        codeToRun = `(function() { ${code} })()`;
      } else {
        log(`Code appears to be async/Promise-based, executing as-is`);
      }
      
      log(`EXECUTING FINAL CODE:`);
      console.log(codeToRun);
      
      // Execute the code
      const result = vm.runInContext(codeToRun, context);
      log(`Raw execution result type: ${typeof result}`);
      
      return result;
    };

    // Execute the code with the provided arguments
    let result = executeCode(funcInfo.body, args);
    
    // Log the result type
    log(`EXECUTION RESULT TYPE: ${typeof result}`);
    
    // If result is a Promise or Promise-like, await it
    if (isPromiseLike(result)) {
      log(`DETECTED PROMISE RESULT - AWAITING...`);
      try {
        result = await result;
        log(`PROMISE RESOLVED TO:`, result);
      } catch (promiseError) {
        log(`PROMISE REJECTION:`, { 
          error: promiseError.message, 
          stack: promiseError.stack 
        });
        throw promiseError;
      }
    } else {
      log(`EXECUTION RESULT (NOT A PROMISE):`, result);
    }
    
    return result;
    
  } catch (error) {
    stats.errors++;
    log(`ERROR EXECUTING CODE: ${functionId}`, { 
      errorType: error.constructor.name,
      errorMessage: error.message, 
      errorStack: error.stack 
    });
    throw error;
  }
}

/**
 * Start the WebSocket server
 */
function startServer() {
  const server = https.createServer({
    key: fs.readFileSync(SSL_KEY_PATH),
    cert: fs.readFileSync(SSL_CERT_PATH)
  });

  const wss = new WebSocket.Server({ 
    server,
    path: '/ws'
  });

  wss.on('connection', (ws, req) => {
    stats.totalConnections++;
    stats.activeConnections++;

    const connectionId = `conn-${Date.now()}-${stats.totalConnections}`;
    log(`New connection: ${connectionId}`, { ip: req.socket.remoteAddress });

    ws.on('message', async (message) => {
      try {
        const data = JSON.parse(message);
        log(`Received message:`, data);

        const requestId = data.requestId || 'unknown';

        // Handle function registration
        if (data.type === 'register') {
          const { functionId, functionBody, paramNames = [] } = data;
          log(`Processing code registration: ${functionId}`);

          const success = registerFunction(functionId, functionBody, paramNames);
          
          ws.send(JSON.stringify({
            requestId,
            status: 'ok',
            result: { registered: success }
          }));
        }

        // Handle function execution
        else if (data.type === 'execute') {
          const { functionId, args = [] } = data;
          log(`Processing code execution: ${functionId}`);

          try {
            // Now executeFunction is async and will properly await Promises
            const result = await executeFunction(functionId, args);
            
            log(`Final result to send for ${requestId}:`, result);
            
            ws.send(JSON.stringify({ 
              requestId, 
              result,
              markerId: data.markerId
            }));
          } catch (execError) {
            log(`Error executing code:`, { error: execError.message, stack: execError.stack });
            ws.send(JSON.stringify({ requestId, error: execError.message }));
          }
        }

        // Unknown message type
        else {
          log(`Unknown message type received`, { type: data.type });
          ws.send(JSON.stringify({ requestId, error: `Unknown message type: ${data.type}` }));
        }
      } catch (error) {
        log(`Error processing message:`, { error: error.message, message: message.toString().substring(0, 200) });
        stats.errors++;

        try {
          ws.send(JSON.stringify({ error: error.message }));
        } catch (sendError) {
          log(`Error sending error response:`, { error: sendError.message });
        }
      }
    });

    ws.on('close', () => {
      stats.activeConnections--;
      log(`Connection closed: ${connectionId}`);
    });

    ws.send(JSON.stringify({
      type: 'system',
      message: 'Secure WebSocket server connected',
      connectionId,
      timestamp: new Date().toISOString()
    }));
  });

  server.listen(WS_PORT, () => {
    log(`Server listening on port ${WS_PORT}`);
  });

  return wss;
}

// Register a test function
registerFunction('test', 'return "Hello, World!";', []);

// Start server when run directly
if (require.main === module) {
  log('Starting server...');
  startServer();
}

// Export functions
module.exports = {
  startServer,
  registerFunction,
  executeFunction
};